

package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;

import entity.Student;
import entity.TbClass;
import service.StudentService;
import service.TbClassService;



/**
 * Servlet implementation class XueshengServlet
 */
@WebServlet("/xuesheng")
public class XueshengServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentService studentService=new StudentService();
	private TbClassService tbClassService=new TbClassService();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		List<TbClass> bjs = tbClassService.findAll();
		request.setAttribute("bjs", bjs);
		String method = request.getParameter("method");
		if(method==null) {
			list(request,response);
		}else if("table".contentEquals(method)) {
			table(request,response);
		}else if("add".contentEquals(method)) {
			add(request,response);
		}else if("update".contentEquals(method)) {
			update(request,response);
		}else if("delete".contentEquals(method)) {
			delete(request,response);
		}else if("save".contentEquals(method)) {
			save(request,response);
		}
		
	}
	
	
                    

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=null;
		String idStr = request.getParameter("id");
		if(idStr!=null&&!"".contentEquals(idStr)) {
			id=Integer.valueOf(idStr);
		}
		String bjIdStr = request.getParameter("bjId");
		Integer bjId = Integer.valueOf(bjIdStr);
		String name=request.getParameter("name");
		String stuNo = request.getParameter("stuNo");
		String password = request.getParameter("password");
		Student model =new Student(id, stuNo,name,  tbClassService.findById(bjId) ,password);
		studentService.save(model);
		response.sendRedirect("xuesheng?bjId="+bjId);
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		Integer id=Integer.valueOf(idStr);
		Integer bjId=studentService.findById(id).getTbclass().getId();
		studentService.delete(id);
		response.sendRedirect("xuesheng?bjId="+bjId);
		
	}
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String idStr = request.getParameter("id");
			Integer id=Integer.valueOf(idStr);
			Student model = studentService.findById(id);
			request.setAttribute("model", model);
			request.getRequestDispatcher("jsp/student/edit.jsp").forward(request, response);
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bjIdStr = request.getParameter("bjId");
		Integer bjId=Integer.valueOf(bjIdStr);
		Student model=new Student();
		model.setTbclass(tbClassService.findById(bjId));
		request.setAttribute("model", model);
		request.getRequestDispatcher("jsp/student/edit.jsp").forward(request, response);
		
	}

	private void table(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		Integer id= Integer.valueOf(idStr);
		List<Student> list=studentService.findByTbClassId(id);
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/student/table.jsp").forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer bjId=null;
		String bjIdStr = request.getParameter("bjId");
		if(bjIdStr!=null&&!"".contentEquals(bjIdStr)) {
			bjId=Integer.valueOf(bjIdStr);
			
		}
		List<TbClass> bjs = tbClassService.findAll();
		request.setAttribute("bjs", bjs);
		request.setAttribute("bjId", bjId);
		if(bjId!=null) {
			request.setAttribute("students", studentService.findByTbClassId(bjId));
		}
		
		request.getRequestDispatcher("jsp/student/list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
